package net.yebaihe.wc;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;

public abstract class DataMan {

	enum MSGTYPE {LOGIN, REGISTER,UPDATE_IMAGE,UPDATE_LOCATION,ADD_SESSION, ADD_ORDER,CHECK_NOTIFICATIONS, LOGOUT, CONFIRM_INVITE, CONFIRM_ORDER,CITY_CHANGE, TALK};
	
	private static DataMan self=null;
	protected Context ctx;
	protected StateMachineInterface machine;
	public static DataMan get(Context machine) {
		if (self==null){
			self=new OfflineDataMan();
			self.ctx=machine;
			self.initdata();
		}
		else{
			self.ctx=machine;
		}
		self.machine=(StateMachineInterface) machine;
		return self;
	}
	public abstract void initdata();
	public void login(String email, String passwd) {
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("email", email);
		map.put("passwd", passwd);
		SendNormalRequest(MSGTYPE.LOGIN,map);
	}
	protected abstract void SendNormalRequest(MSGTYPE login, HashMap<String, String> params);
	public void register(String email, String username, String passwd) {
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("email", email);
		map.put("username", username);
		map.put("passwd", passwd);
		SendNormalRequest(MSGTYPE.REGISTER,map);
	}
	public void updateCurrUserImageInfo(Bitmap bmp) {
		final ByteArrayOutputStream os = new ByteArrayOutputStream();  
		bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
		HashMap <String,String>map=new HashMap <String,String>();
		WCApp app=(WCApp) self.ctx.getApplicationContext();
		UserInfoData userinfo = MyHelper.get(ctx).getCurrUserInfoData(app.email);
		map.put("id", ""+userinfo._id);
		map.put("image", Base64.encodeBytes(os.toByteArray()));
		SendNormalRequest(MSGTYPE.UPDATE_IMAGE,map);
	}
	public void updateMyLocation(String email, double lon, double lat){
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("email", email);
		map.put("lon", ""+lon);
		map.put("lat", ""+lat);
		SendNormalRequest(MSGTYPE.UPDATE_LOCATION,map);
	}
	public void updateDishImage(int id, String string) {
		/*if (string==null){
			Toast.makeText(ctx, "Cache fail!", Toast.LENGTH_SHORT).show();
			return;
		}
		final ByteArrayOutputStream os = new ByteArrayOutputStream();  
		string.compress(Bitmap.CompressFormat.PNG, 100, os);*/
		SQLiteDatabase db = MyHelper.get(ctx).getReadableDatabase();
		if (db!=null){
			ContentValues initValues = new ContentValues();
			try {
				initValues.put("thumbnail",string);
				db.update("dishes", initValues, "_id="+id, null);
			} 
			finally{
				db.close();
			}
		}
		
	}
	public void addNewSession(long id, long restaurant) {
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("uid", ""+id);
		map.put("restaurant", ""+restaurant);
		SendNormalRequest(MSGTYPE.ADD_SESSION,map);
	}
	public void addOrder(int sessionid, long dishid, long id) {
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("session", ""+sessionid);
		map.put("dish", ""+dishid);
		map.put("uid", ""+id);
		SendNormalRequest(MSGTYPE.ADD_ORDER,map);
	}
	public void checkNotifies() {
		HashMap <String,String>map=new HashMap <String,String>();
		SendNormalRequest(MSGTYPE.CHECK_NOTIFICATIONS,map);
	}
	public void logout() {
		HashMap <String,String>map=new HashMap <String,String>();
		SendNormalRequest(MSGTYPE.LOGOUT,map);
	}
	public void confirmInvite(long _id, long sessionid) {
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("user", ""+_id);
		map.put("session", ""+sessionid);
		SendNormalRequest(MSGTYPE.CONFIRM_INVITE,map);
	}
	public void confirmOrder(int sessionid,String title) {
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("session", ""+sessionid);
		map.put("title", ""+title);
		SendNormalRequest(MSGTYPE.CONFIRM_ORDER,map);
	}
	public void insertTalkCnt(long _id, int sessionid, String cnt) {
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("session", ""+sessionid);
		map.put("cnt", cnt);
		map.put("user", ""+_id);
		SendNormalRequest(MSGTYPE.TALK,map);
	}

}
